package com.kang;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.kang.bean.enums.bot.PlayEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: service
 * @description:
 * @author: K.faWu
 * @create: 2022-06-10 14:18
 **/

public class CommonsUtils {

    public static <T> QueryWrapper<T> selectCode(String accountCode, String groupCode) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(PlayEnum.ACCOUNT_CODE, accountCode);
        queryWrapper.eq(PlayEnum.GROUP_CODE, groupCode);
        return queryWrapper;
    }


    /**
     * 资源计算公式
     * 招募一个士兵需要消耗 5 粮食 5 矿石 2 木材
     * 招募一个矿工/伐木工需要消耗 5袋粮食 2块矿石 2根木材
     *
     * @param num  招募数量
     * @param type 招募类型
     */
    public static Map<String, Integer> recruitConsume(Integer num, String type) {
        Map<String, Integer> map = new HashMap<>(3);
        if (Constants.SOLDIER.equals(type)) {
            //一个士兵需要消耗5个矿石
            map.put(Constants.MINE, num * 5);
        } else if (Constants.WOODCUTTER.equals(type) || Constants.MINER.equals(type)) {
            //若是招募矿工或者伐木工则消耗2个矿石
            map.put(Constants.MINE, num * 2);
        } else {
            return null;
        }
        map.put(Constants.WOOD, num * 2);
        map.put(Constants.FOOD, num * 5);
        return map;
    }

    public static Map<String, Object> catCodeToMap(String catCode) {
        catCode = catCode.replace("[", "{").replace("]", "}").replaceAll(":", "=");
        return mapStringToMap(catCode);
    }

    public static Map<String, Object> mapStringToMap(String str) {
        str = str.substring(1, str.length() - 1);
        String[] strs = str.split(",");
        Map<String, Object> map = new HashMap<>(8);
        for (String string : strs) {
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            map.put(key, value);
        }
        return map;
    }
}
